Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./year2.RDS")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2021-06-30"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2021-06-30"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.25, n = 372)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 11.48127 11.52595 11.57001 11.61346 11.65633 11.69863 11.74038 11.78159
##   [9] 11.82229 11.86248 11.90219 11.94144 11.98023 12.01859 12.05653 12.09407
##  [17] 12.13121 12.16790 12.20410 12.23979 12.27493 12.30949 12.34342 12.37670
##  [25] 12.40929 12.44116 12.47226 12.50308 12.53397 12.56473 12.59515 12.62503
##  [33] 12.65416 12.68234 12.70936 12.73501 12.76042 12.78664 12.81335 12.84020
##  [41] 12.86688 12.89305 12.91840 12.94259 12.96529 12.98618 13.00493 13.02121
##  [49] 13.03638 13.05183 13.06728 13.08245 13.09706 13.11082 13.12346 13.13469
##  [57] 13.14423 13.15179 13.15711 13.15989 13.15986 13.15672 13.14979 13.13900
##  [65] 13.12506 13.10867 13.09052 13.07132 13.05177 13.03256 13.01441 12.99349
##  [73] 12.96632 12.93404 12.89779 12.85873 12.81801 12.77677 12.73617 12.69735
##  [81] 12.66147 12.62966 12.60310 12.57779 12.54963 12.51958 12.48856 12.45753
##  [89] 12.42743 12.39922 12.37382 12.35220 12.33315 12.31487 12.29736 12.28062
##  [97] 12.26467 12.24949 12.23511 12.22153 12.20874 12.19677 12.18560 12.17525
## [105] 12.16573 12.15703 12.14984 12.14465 12.14123 12.13932 12.13867 12.13904
## [113] 12.14018 12.14183 12.14376 12.14571 12.14743 12.14867 12.15093 12.15548
## [121] 12.16174 12.16915 12.17714 12.18513 12.19256 12.19885 12.20343 12.20736
## [129] 12.21199 12.21717 12.22274 12.22855 12.23445 12.24028 12.24591 12.25116
## [137] 12.25589 12.25996 12.26320 12.26476 12.26417 12.26188 12.25834 12.25400
## [145] 12.24929 12.24466 12.24057 12.23745 12.23575 12.23591 12.23839 12.24363
## [153] 12.25207 12.26210 12.27198 12.28192 12.29215 12.30289 12.31436 12.32678
## [161] 12.34038 12.35538 12.37375 12.39674 12.42343 12.45293 12.48435 12.51676
## [169] 12.54929 12.58102 12.61106 12.63851 12.66246 12.68201 12.70202 12.72733
## [177] 12.75703 12.79024 12.82606 12.86360 12.90195 12.94022 12.97751 13.01293
## [185] 13.04558 13.07457 13.09900 13.11797 13.13058 13.13595 13.13575 13.13243
## [193] 13.12620 13.11727 13.10586 13.09217 13.07643 13.05885 13.03963 13.01900
## [201] 12.99717 12.97435 12.95075 12.92660 12.89779 12.86102 12.81767 12.76910
## [209] 12.71669 12.66183 12.60588 12.55023 12.49625 12.44531 12.39879 12.35808
## [217] 12.31841 12.27491 12.22874 12.18107 12.13307 12.08591 12.04076 11.99878
## [225] 11.96115 11.92539 11.88852 11.85096 11.81311 11.77540 11.73824 11.70204
## [233] 11.66723 11.63421 11.60340 11.57522 11.55007 11.52648 11.50285 11.47945
## [241] 11.45652 11.43433 11.41314 11.39319 11.37476 11.35811 11.34348 11.33113
## [249] 11.32133 11.31434 11.31040 11.30951 11.31127 11.31539 11.32156 11.32950
## [257] 11.33890 11.34946 11.36089 11.37289 11.38889 11.41167 11.44001 11.47263
## [265] 11.50829 11.54574 11.58373 11.62101 11.65633 11.68843 11.71606 11.73798
## [273] 11.75912 11.78443 11.81262 11.84245 11.87265 11.90195 11.92910 11.95283
## [281] 11.97188 11.98702 12.00007 12.01134 12.02115 12.02981 12.03762 12.04491
## [289] 12.05198 12.05915 12.06672 12.07501 12.08434 12.09500 12.10732 12.11937
## [297] 12.12928 12.13746 12.14435 12.15036 12.15591 12.16142 12.16731 12.17399
## [305] 12.18189 12.19142 12.20301 12.21684 12.23261 12.25004 12.26882 12.28866
## [313] 12.30926 12.33034 12.35158 12.37271 12.39342 12.41341 12.43239 12.45007
## [321] 12.46615 12.48323 12.50346 12.52588 12.54952 12.57341 12.59658 12.61807
## [329] 12.63691 12.65213 12.66454 12.67571 12.68577 12.69487 12.70316 12.71077
## [337] 12.71785 12.72455 12.73101 12.73737 12.74379 12.75039 12.75672 12.76226
## [345] 12.76710 12.77132 12.77501 12.77826 12.78116 12.78378 12.78622 12.78832
## [353] 12.78988 12.79091 12.79145 12.79149 12.79108 12.79022 12.78894 12.78725
## [361] 12.78517 12.78273 12.77994 12.77684 12.77346 12.76977 12.76573 12.76133
## [369] 12.75654 12.75133 12.74567 12.73954
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./site_objects/wrf_a_year2.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.25, n = 372)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 10.75353 10.84131 10.92742 11.01184 11.09456 11.17556 11.25484 11.33238
##   [9] 11.40816 11.48216 11.55438 11.62481 11.69341 11.76019 11.82513 11.88820
##  [17] 11.94943 12.00883 12.06645 12.12231 12.17645 12.22889 12.27966 12.32880
##  [25] 12.37634 12.42230 12.46673 12.50928 12.54969 12.58812 12.62470 12.65956
##  [33] 12.69284 12.72469 12.75524 12.78463 12.81201 12.83659 12.85863 12.87838
##  [41] 12.89611 12.91205 12.92648 12.93964 12.95179 12.96319 12.97409 12.98474
##  [49] 12.99357 12.99911 13.00177 13.00195 13.00007 12.99655 12.99179 12.98620
##  [57] 12.98020 12.97419 12.96859 12.96382 12.96027 12.95836 12.95789 12.95812
##  [65] 12.95868 12.95920 12.95931 12.95866 12.95686 12.95355 12.94836 12.94151
##  [73] 12.93353 12.92453 12.91459 12.90382 12.89231 12.88016 12.86747 12.85433
##  [81] 12.84085 12.82712 12.81324 12.79933 12.78538 12.77127 12.75687 12.74208
##  [89] 12.72678 12.71084 12.69416 12.67661 12.65744 12.63614 12.61300 12.58827
##  [97] 12.56223 12.53514 12.50726 12.47887 12.45024 12.42162 12.39330 12.36553
## [105] 12.33859 12.31274 12.28373 12.24813 12.20747 12.16329 12.11711 12.07046
## [113] 12.02486 11.98185 11.94295 11.90969 11.88359 11.86619 11.85445 11.84429
## [121] 11.83573 11.82877 11.82339 11.81959 11.81739 11.81677 11.81773 11.82165
## [129] 11.82954 11.84081 11.85485 11.87110 11.88894 11.90780 11.92709 11.94621
## [137] 11.96458 11.98161 11.99670 12.01298 12.03356 12.05785 12.08526 12.11520
## [145] 12.14708 12.18031 12.21431 12.24847 12.28222 12.31496 12.34610 12.37505
## [153] 12.40123 12.42826 12.45936 12.49337 12.52908 12.56533 12.60093 12.63469
## [161] 12.66544 12.69199 12.71561 12.73840 12.76039 12.78164 12.80220 12.82211
## [169] 12.84142 12.86017 12.87842 12.89621 12.91359 12.93060 12.94914 12.97071
## [177] 12.99476 13.02075 13.04812 13.07634 13.10485 13.13311 13.16058 13.18671
## [185] 13.21095 13.23276 13.25159 13.26689 13.27812 13.28474 13.28839 13.29105
## [193] 13.29259 13.29292 13.29194 13.28952 13.28557 13.27999 13.27266 13.26348
## [201] 13.25235 13.23915 13.22379 13.20616 13.18436 13.15712 13.12526 13.08963
## [209] 13.05105 13.01035 12.96836 12.92590 12.88382 12.84293 12.80407 12.76807
## [217] 12.73054 12.68755 12.64054 12.59097 12.54031 12.49002 12.44154 12.39635
## [225] 12.35590 12.31641 12.27359 12.22818 12.18088 12.13244 12.08356 12.03497
## [233] 11.98741 11.94158 11.89821 11.85803 11.82176 11.78750 11.75305 11.71866
## [241] 11.68460 11.65111 11.61845 11.58689 11.55667 11.52806 11.50131 11.47669
## [249] 11.45444 11.43482 11.41809 11.40358 11.39049 11.37890 11.36889 11.36054
## [257] 11.35394 11.34917 11.34632 11.34545 11.34764 11.35354 11.36264 11.37443
## [265] 11.38839 11.40400 11.42075 11.43812 11.45561 11.47268 11.48883 11.50354
## [273] 11.51934 11.53857 11.56042 11.58409 11.60878 11.63369 11.65801 11.68095
## [281] 11.70170 11.72247 11.74579 11.77128 11.79856 11.82724 11.85697 11.88736
## [289] 11.91804 11.94862 11.97874 12.00803 12.03609 12.06256 12.08707 12.11037
## [297] 12.13348 12.15636 12.17900 12.20137 12.22346 12.24523 12.26668 12.28777
## [305] 12.30848 12.32880 12.34870 12.36832 12.38781 12.40713 12.42625 12.44515
## [313] 12.46380 12.48216 12.50020 12.51789 12.53521 12.55212 12.56859 12.58460
## [321] 12.60011 12.61616 12.63352 12.65173 12.67032 12.68885 12.70685 12.72386
## [329] 12.73944 12.75311 12.76494 12.77541 12.78473 12.79309 12.80072 12.80780
## [337] 12.81454 12.82115 12.82783 12.83479 12.84222 12.85034 12.85847 12.86590
## [345] 12.87276 12.87917 12.88525 12.89115 12.89697 12.90285 12.90892 12.91496
## [353] 12.92068 12.92610 12.93126 12.93616 12.94083 12.94530 12.94959 12.95372
## [361] 12.95772 12.96160 12.96539 12.96915 12.97289 12.97658 12.98019 12.98368
## [369] 12.98703 12.99020 12.99316 12.99587
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./site_objects/wrf_b_year2.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.25, n = 372)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 10.85596 10.91638 10.97578 11.03413 11.09145 11.14770 11.20287 11.25697
##   [9] 11.30997 11.36186 11.41263 11.46227 11.51077 11.55811 11.60429 11.64934
##  [17] 11.69330 11.73619 11.77801 11.81876 11.85845 11.89709 11.93469 11.97125
##  [25] 12.00678 12.04128 12.07476 12.10685 12.13729 12.16626 12.19394 12.22051
##  [33] 12.24614 12.27103 12.29533 12.31925 12.34253 12.36487 12.38628 12.40678
##  [41] 12.42640 12.44517 12.46311 12.48025 12.49660 12.51220 12.52706 12.54122
##  [49] 12.55497 12.56850 12.58169 12.59441 12.60653 12.61793 12.62849 12.63806
##  [57] 12.64653 12.65377 12.65965 12.66404 12.66682 12.66787 12.66671 12.66325
##  [65] 12.65787 12.65097 12.64295 12.63420 12.62511 12.61608 12.60750 12.59783
##  [73] 12.58550 12.57091 12.55445 12.53650 12.51745 12.49770 12.47763 12.45764
##  [81] 12.43811 12.41943 12.40200 12.38246 12.35815 12.33047 12.30082 12.27061
##  [89] 12.24122 12.21408 12.19057 12.17211 12.15706 12.14283 12.12935 12.11656
##  [97] 12.10442 12.09285 12.08179 12.07120 12.06101 12.05116 12.04159 12.03224
## [105] 12.02306 12.01398 12.00528 11.99723 11.98975 11.98279 11.97627 11.97014
## [113] 11.96432 11.95876 11.95338 11.94813 11.94293 11.93772 11.93372 11.93183
## [121] 11.93154 11.93230 11.93358 11.93487 11.93561 11.93530 11.93338 11.93014
## [129] 11.92630 11.92199 11.91732 11.91245 11.90750 11.90259 11.89786 11.89345
## [137] 11.88947 11.88607 11.88337 11.87913 11.87148 11.86113 11.84876 11.83509
## [145] 11.82080 11.80660 11.79318 11.78124 11.77148 11.76460 11.76130 11.76227
## [153] 11.76821 11.77811 11.79025 11.80428 11.81984 11.83659 11.85418 11.87225
## [161] 11.89045 11.90842 11.92869 11.95346 11.98193 12.01331 12.04680 12.08160
## [169] 12.11691 12.15195 12.18590 12.21799 12.24739 12.27333 12.30065 12.33406
## [177] 12.37261 12.41532 12.46123 12.50937 12.55877 12.60846 12.65748 12.70485
## [185] 12.74962 12.79080 12.82744 12.85856 12.88320 12.90038 12.91290 12.92411
## [193] 12.93391 12.94221 12.94889 12.95386 12.95702 12.95826 12.95748 12.95459
## [201] 12.94947 12.94204 12.93218 12.91980 12.90214 12.87731 12.84648 12.81081
## [209] 12.77148 12.72967 12.68653 12.64325 12.60100 12.56094 12.52425 12.49210
## [217] 12.45854 12.41810 12.37270 12.32426 12.27467 12.22587 12.17975 12.13824
## [225] 12.10324 12.06962 12.03170 11.99055 11.94723 11.90281 11.85837 11.81497
## [233] 11.77368 11.73558 11.70172 11.67319 11.65105 11.63523 11.62450 11.61820
## [241] 11.61566 11.61622 11.61921 11.62397 11.62984 11.63615 11.64224 11.64744
## [249] 11.65108 11.65251 11.65106 11.64996 11.65229 11.65720 11.66383 11.67131
## [257] 11.67880 11.68543 11.69034 11.69267 11.69254 11.69089 11.68813 11.68466
## [265] 11.68089 11.67722 11.67405 11.67179 11.67084 11.67161 11.67450 11.67992
## [273] 11.68588 11.69050 11.69429 11.69776 11.70142 11.70579 11.71138 11.71870
## [281] 11.72828 11.74039 11.75476 11.77106 11.78894 11.80808 11.82812 11.84873
## [289] 11.86958 11.89032 11.91062 11.93013 11.94853 11.96547 11.98061 11.99625
## [297] 12.01453 12.03494 12.05695 12.08001 12.10362 12.12723 12.15032 12.17236
## [305] 12.19282 12.21118 12.22690 12.24028 12.25211 12.26256 12.27180 12.28003
## [313] 12.28741 12.29413 12.30037 12.30630 12.31211 12.31798 12.32408 12.33060
## [321] 12.33771 12.34492 12.35166 12.35798 12.36393 12.36956 12.37493 12.38010
## [329] 12.38510 12.39001 12.39503 12.40026 12.40561 12.41098 12.41626 12.42135
## [337] 12.42615 12.43055 12.43446 12.43777 12.44038 12.44218 12.44364 12.44520
## [345] 12.44672 12.44805 12.44907 12.44963 12.44959 12.44883 12.44719 12.44490
## [353] 12.44224 12.43920 12.43578 12.43194 12.42769 12.42300 12.41787 12.41227
## [361] 12.40620 12.39964 12.39258 12.38498 12.37685 12.36819 12.35903 12.34939
## [369] 12.33930 12.32877 12.31782 12.30648
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./site_objects/wrf_c_year2.rda")

keeping in case

#save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
#save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
#save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
#save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
#save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
#save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
#save(both_ymina, file = "./plotly_objs/both_ymina.rda")
#save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

#save(both_yminb, file = "./plotly_objs/both_yminb.rda")
#save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

#save(both_yminc, file = "./plotly_objs/both_yminc.rda")
#save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")